package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import javax.websocket.server.PathParam;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    //注入mapper对象
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    public void save(SetmealDTO setmealDTO) {
        //保存套餐基本信息到setmeal表
        //DTO转实体
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //调用mapper层保存套餐基本信息
        setmealMapper.insert(setmeal);

        //获取套餐保存生成的id
        Long setmealId = setmeal.getId();

        //保存套餐和菜品的关联信息到setmeal_dish表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //遍历设置套餐id
        setmealDishes.forEach(setmealDish->{
            setmealDish.setSetmealId(setmealId);
        });
        //批量插入
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开启分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        //调用mapper层查询套餐列表
        Page<SetmealVO> page = setmealMapper.page(setmealPageQueryDTO);

        //通过查询到的

        //获取total和recodes
        long total = page.getTotal();
        List<SetmealVO> result = page.getResult();

        //封装返回结果
        return PageResult.builder().total(total).records(result).build();
    }

    /**
     * 根据id批量删除套餐
     * @param ids
     */
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //判断是否有套餐属于在售状态,若在售无法删除
        //遍历ids集合
        ids.forEach( id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(setmeal.getStatus() == 1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });

        //删除setmeal_dish中与套餐关联的菜品
        setmealDishMapper.deleteBySetmealIds(ids);

        //调用mapper层批量删除
        setmealMapper.deleteByIds(ids);

    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //从setmeal表，查询套餐基本信息
        Setmeal setmeal = setmealMapper.getById(id);

        //从setmeal_dish表，查询套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);

        //封装VO对象
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        //返回结果
        return setmealVO;
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        //修改套餐基本信息
        //DTO转实体
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);

        //删除原套餐关联菜品数据，重新插入
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //遍历setmealDishes集合
        setmealDishes.forEach( setmealDish -> {
            //设置套餐id
            setmealDish.setSetmealId(setmeal.getId());
        });

        //批量插入套餐关联菜品数据
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 根据id启售或停售套餐
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //检查套餐中是否存在停售的菜品，若存在则无法起售
        if(status == StatusConstant.ENABLE){
            List<SetmealDish> setmealDishs = setmealDishMapper.getBySetmealId(id);
            setmealDishs.forEach( setmealDish -> {
                //根据菜品id查dish表，检查菜品状态
                Dish dish = dishMapper.getById(setmealDish.getDishId());
                if(dish.getStatus() == StatusConstant.DISABLE){
                    throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }

        //调用mapper层修改套餐状态
        Setmeal setmeal = Setmeal.builder().id(id).status(status).build();
        setmealMapper.update(setmeal);

    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
